var app = getApp();
import ajaxhelper from '../../utils/ajaxhelper.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1, //预设当前项的值
    dataList: [],//列表数据
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    pageSize: 10,
    pageNo: 1,
    isShow: false,//是否显示没有列表的提示图
    subjectId: '',//类型id
    name: '',
    totalPrice: '',
    type: '',
    showView: false,
    arr:[],
    mediaType:'',
    iseditor:true,
    item:'编辑',
    brr:[],
    i:'',
    index:'',
    currentId:'',
    ishide:true,
    image:'../../images/delete.png',
    idx:'',
    crr:[],
    drr:[],
    num:0,
    err:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log(option)
    this.getListData(this.data.currentTab); //获取列表数据
  },

  //列表渲染数据
  getListData: function (id) {
    var type;
    if (id == '1') {
      type = 'AUDIO'
    } else if (id == '2') {
      type = 'VIDEO'
    } 
    let param = {
      "mediaType": type,
      "pageSize": this.data.pageSize,
      "pageNo": this.data.pageNo,
    }
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "knowledge/myFavorite/" + this.data.pageNo + "/" + this.data.pageSize +"?token=" + encodeURIComponent(token), param, this, this.setListData)
  },
  setListData(res){
    if (res.result == true){
      
      var Data = res.data;
      var Result = Data.result || '';
      var length = Result.length;
      var pageNo = this.data.pageNo;
      var hasmore = Data.totalCount - pageNo * this.data.pageSize;
      this.setData({
        pageNo: ++pageNo,
        hasRefresh: false,
        isGetMore: false,
        hasMore: hasmore,
        brr:[]
        // tip: '上拉加载更多数据'
      })
      
      if (Data && Result) {
        for (var i = 0; i < Result.length;i++){
          this.data.brr.push(false);
        }

        this.setData({
          dataList: this.data.dataList.concat(Result),
        })
      }
    }else {
      this.setData({
        hasRefresh: false,
        isGetMore: false,
      })
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    this.setData({
      isShow: true
    })
  }
  ,

  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    console.log(this.data.mediaType)
    var that = this;
    var id = e.currentTarget.dataset.id;
    var crr = this.data.crr;
    if (this.data.currentTab == id) { return false; }
    else {
      this.setData({
        currentTab: id,
        dataList: [],
        pageNo: 1,
        showView:false,
        item:'编辑',
        image:'../../images/delete.png',
        crr:[], 
        brr:[],
        arr:[],
        num:0      
      })
      if (!this.data.iseditor){
        this.setData({
          iseditor: (!this.data.iseditor)
        })
      }
    }
    this.getListData(id);//获取列表数据
  },

  // 点击编辑
  editor:function(event){
    console.log(this.data.item)
    var item = event.currentTarget.dataset.item;
    var arr = this.data.arr;
    var brr = this.data.brr;
    var num = this.data.num;
    this.setData({
      arr:[],
      brr:[],
      num:0,
      showView: (!this.data.showView),     
      item:item,
      iseditor: (!this.data.iseditor)
    })
  },
  
  // 点击列表圆圈
  deleted: function (event){
    console.log(this.data.image)
    var dataList = this.data.dataList;
    console.log(dataList)
    var length = dataList.length;
    var id = event.currentTarget.dataset.id;
    var mediaType = event.currentTarget.dataset.mediaType;
    var idx = event.target.dataset.index;
    var brr = this.data.brr;
    var arr = this.data.arr;
    var ishide = this.data.ishide;
    var image = this.data.image;

    if (this.data.brr[idx] = !this.data.brr[idx]) {
      this.data.brr[idx] = true
      this.data.num++
    } else {
      this.data.num--
    } 

    if(brr==length){
      if(this.data.brr[idx] =!this.data.brr[idx]){
        this.data.brr[idx] = false
        this.setData({
          num: this.data.num--,
          brr:brr,
          arr:[],
          ishide:false,
          image:"../../images/delete.png"
        })
      }     
    }
    console.log(this.data.image)
       
      this.setData({       
        mediaType: mediaType,
        brr: brr,
        num:this.data.num,
        arr:[],
        ishide:false,
        image: "../../images/delete.png"
      })
      console.log(this.data.image)  
  },

  //点击删除
  delete:function(){
    console.log(this.data.crr)
    var dataList = this.data.dataList;
    var brr = this.data.brr;
    console.log(this.data.brr)
    for(var i=0; i<dataList.length; i++){
      var id = dataList[i].favId;
      this.data.crr.push(id);
    }
    console.log(this.data.crr)
    for(var i=0; i<brr.length; i++){
      if (this.data.brr[i]){
        this.data.drr.push(i)       
      }
    }
    console.log(this.data.drr)
    for (var i = 0; i < this.data.drr.length; i++) {
      var val = this.data.drr[i]
      var id = dataList[val].favId
      this.setData({
          arr: this.data.arr.concat(id),
        })
    }


    let param = JSON.stringify(this.data.arr)
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "favorite/deleteList?token=" + encodeURIComponent(token), param, this, this.set)
  },
  set(res){
    console.log(res)
    this.setData({
      dataList:[],
      arr:[],
      crr:[],
      brr:[],
      drr:[],
      num:0
    })
    if (this.data.mediaType == 'AUDIO'){
      this.getListData(1)
    }else{
      this.getListData(2)
    }
    
  },

  //点击全选
  choiced:function(event){
    if(this.data.brr){
      this.setData({
        brr:[],
        num:0
      })
    }
    var data = this.data.dataList;
    var mediaType = event.currentTarget.dataset.mediaType;
    var image = event.currentTarget.dataset.image;
    console.log(image)
    this.setData({
      mediaType: mediaType,
      ishide: (!this.data.ishide),
      image: image, 
      brr:[],
    })
    if (!this.data.ishide){
      for (var idx = 0; idx < data.length; idx++) {
        var favId = data[idx].favId;
        this.data.brr[idx] = true;
        this.data.num++;
        this.setData({
          arr: this.data.arr.concat(favId),
          brr: this.data.brr,
          num:this.data.num
        })
      }
    }else{
      for (var idx = 0; idx < data.length; idx++) {
        var favId = data[idx].favId;
        this.data.brr[idx] = false;
        this.data.num--;
        this.setData({
          brr: this.data.brr,
          arr:[],
          num: this.data.num
        })
      }
    }
  },

  //点击下箭头 打开弹窗
  topopup:function(event){
    var that = this;
    var id = event.currentTarget.dataset.id;
    var err =this.data.err.concat(id);
    var mediaType = event.currentTarget.dataset.mediaType;
    console.log(mediaType)
    this.setData({
      err:err
    })
    wx.showModal({
      title: '取消订阅',
      // content: '取消订阅',
      confirmColor: '#FF4612',
      success:function(res){
        if(res.confirm){
          console.log('用户点击确定')               
          that.todeleted(that.data.err, mediaType)
          console.log(that.data.err)
        } else if (res.cancel){
          console.log('用户点击取消')
          that.tocancel(that.data.err)
        }
      }
    })
  },

  //去取消
  tocancel:function(err){
    this.setData({
      err:[]
    })
  },

  //去删除
  todeleted: function (err, mediaType){

    console.log(err)
    console.log(mediaType)
    this.setData({
      mediaType: mediaType,
      dataList:[]
    })
    let param = JSON.stringify(err)
    console.log(param)
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "favorite/deleteList?token=" + encodeURIComponent(token), param, this, this.setDel)
  },
  setDel:function(res){
    if(res.result == true){
      if (this.data.mediaType == 'AUDIO') {
        this.getListData(1)
      } else {
        this.getListData(2)
      }
    }
  },

 //跳转专辑详情
  toalbum:function(event){
    var id = event.currentTarget.dataset.id; 
    var mediaType = event.currentTarget.dataset.mediaType;
    console.log(id)
    console.log(mediaType)
    if (mediaType =='AUDIO'){
      wx.navigateTo({
        url: '../audioAlbumDetail/audioAlbumDetail?id=' + id
      })
    }else{
      wx.navigateTo({
        url: '../videoAlbumDetail/videoAlbumDetail?id=' + id
      })
    }
  },

  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      isShow: false
    })
    if (this.data.hasRefresh) return false;
    this.setData({
      dataList: [],
      hasRefresh: true,
      pageNo: 1
    })
    this.getListData(this.data.currentTab)
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  //上拉加载
  onReachBottom() {
    if (this.data.isGetMore) return false;
    if (this.data.hasRefresh) return false;
    if (this.data.hasMore > 0) {
      this.setData({
        isGetMore: true,
        // tip: '正在加载中...'
      })
      this.getListData(this.data.currentTab);
    } else {
      this.setData({
        isGetMore: false,
        // tip: '没有更多数据了...'
      })
      return false
    }
  },
})